#include <logbase.h>
#include <genrand.h>
#include <cirqueue.h>
#include <cstring.h>

struct cqChkStc
{
    int id;
    struct cstring name;
    struct cstring addr;
};
void *cqChkAlloc(int cfgval)
{
    struct cqChkStc *tmpdt = (struct cqChkStc *)malloc(sizeof(struct cqChkStc));
    tmpdt->id = cfgval;
    tmpdt->name = cstrInitVal;
    tmpdt->addr = cstrInitVal;
    cstrSprint(&tmpdt->name, 32, "lxname-%d", tmpdt->id);
    cstrSprint(&tmpdt->addr, 32, "lxaddr-%d", tmpdt->id);
    logdebugc("alloc val:%d,%s,%s", tmpdt->id, tmpdt->name.str, tmpdt->addr.str);
    return tmpdt;
}
void cqChkFree(struct cqChkStc *mydata)
{
    struct cqChkStc *tmpdt = (struct cqChkStc *)(mydata);
    // logdebugc("free val:%d,%s,%s", tmpdt->id, tmpdt->name.str, tmpdt->addr.str);
    cstrFree(&tmpdt->name);
    cstrFree(&tmpdt->addr);
    free(tmpdt);
}
int cqChkCmpStc(const void *a, const void *b)
{
    struct cqChkStc *tmpa = *((struct cqChkStc **)(a));
    struct cqChkStc *tmpb = *((struct cqChkStc **)(b));
    return tmpa->id - tmpb->id;
}

int cqChkCmpLong(const void *a, const void *b)
{
    long tmpa = (long)*(void **)a;
    long tmpb = (long)*(void **)b;
    return tmpa - tmpb;
}
int main(int argc, char **argv)
{
    cCirQueRoot tmpCqRoot;
    {
        cCirQueInit(&tmpCqRoot, NULL, 1);
        for (long i = 0; i < 5; i++)
        {
            cCirQuePushBack(&tmpCqRoot, (void *)i);
        }
        for (long i = 10; i < 15; i++)
        {
            cCirQuePushFront(&tmpCqRoot, (void *)i);
        }
        while (1)
        {
            long tmpval;
            if (!cCirQuePullFront(&tmpCqRoot, (void **)&tmpval))
            {
                break;
            }
            logdebugc("get front:%ld", tmpval);
        }
        cCirQueFree(&tmpCqRoot);
    }
    {
        cCirQueInit(&tmpCqRoot, NULL, 1);
        for (long i = 0; i < 5; i++)
        {
            cCirQuePushBack(&tmpCqRoot, (void *)i);
        }
        for (long i = 10; i < 15; i++)
        {
            cCirQuePushFront(&tmpCqRoot, (void *)i);
        }
        while (1)
        {
            long tmpval;
            if (!cCirQuePullBack(&tmpCqRoot, (void **)&tmpval))
            {
                break;
            }
            logdebugc("get front:%ld", tmpval);
        }
        cCirQueFree(&tmpCqRoot);
    }
    {
        cCirQueInit(&tmpCqRoot, cqChkCmpLong, 1);
        for (long i = 0; i < 5; i++)
        {
            cCirQuePushBack(&tmpCqRoot, (void *)i);
        }
        for (long i = 10; i < 15; i++)
        {
            cCirQuePushFront(&tmpCqRoot, (void *)i);
        }
        cCirQueSort(&tmpCqRoot);
        long tmpfind = 10;
        long tmpFid = cCirQueSearch(&tmpCqRoot, (void *)tmpfind);
        logdebugc("find id = %ld", tmpFid);
        while (1)
        {
            long tmpval;
            if (!cCirQuePullFront(&tmpCqRoot, (void **)&tmpval))
            {
                break;
            }
            logdebugc("get front:%ld", tmpval);
        }
        cCirQueFree(&tmpCqRoot);
    }
    {
        cCirQueInit(&tmpCqRoot, cqChkCmpStc, 1);
        for (int i = 0; i < 5; i++)
        {
            void *tmpval = cqChkAlloc(i);
            cCirQuePushBack(&tmpCqRoot, tmpval);
        }
        for (int i = 10; i < 15; i++)
        {
            void *tmpval = cqChkAlloc(i);
            cCirQuePushFront(&tmpCqRoot, tmpval);
        }
        cCirQueSort(&tmpCqRoot);
        struct cqChkStc tmpfind;
        tmpfind.id = 10;
        long tmpFid = cCirQueSearch(&tmpCqRoot, (void *)&tmpfind);
        logdebugc("find id = %ld", tmpFid);
        while (1)
        {
            struct cqChkStc *tmpval;
            if (!(cCirQuePullFront(&tmpCqRoot, &tmpval)))
            {
                break;
            }
            logdebugc("get front:%d,%s,%s", tmpval->id, tmpval->name.str, tmpval->addr.str);
            cqChkFree(tmpval);
        }
        cCirQueFree(&tmpCqRoot);
    }

    return 1;
}
